package com.woniuxy.service.impl;

import com.woniuxy.entity.*;
import com.woniuxy.mapper.*;
import com.woniuxy.service.DetailService;
import com.woniuxy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.*;

@Service
public class DetailServiceImpl implements DetailService {

    @Resource
    private ImgMapper imgMapper;
    @Resource
    private CommunicateMapper communicateMapper;


    @Override
    public void saveDetailAndImg(Communicate communicate, ArrayList<Img> imgs) throws Exception {
        communicateMapper.insert(communicate);
        Integer id = communicate.getCommunicateId();
//        System.out.println(1/0);
        for (Img img : imgs) {
            img.setOtherId(id);
            imgMapper.insertSelective(img);
        }
        System.out.println(id);
    }

    @Override
    public int queryCanSeeDetailCount(Integer userId, Integer campId) {
        long count = communicateMapper.queryCanSeeDetailCount(userId,campId);

        return (int)count;
    }

    @Override
    public List<Map<String, Object>> queryCanSeeDetailLimit(Page page, Integer userId, Integer campId)  throws Exception{
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("campId",campId);
        int number = page.getNumber();
        map.put("number",number);
        map.put("startId",(page.getPageIndex()-1) * number);
        List<Map<String, Object>> list = communicateMapper.queryCanSeeDetailLimit(map);



        Instant now = Instant.now();
        for(Map<String, Object> m : list){

            int islike = 0;
            if(!m.get("total").equals(0)) {
                List<Map<String, Object>> likes = (List<Map<String, Object>>) m.get("like");
                for (Map<String, Object> like : likes) {
                    System.out.println(like.get("userId"));
                    if (like.get("userId").equals(userId)) {
                        islike = 1;
                        break;
                    }
                }
            }
            m.put("islike",islike);

            HashMap<String, Object> content = new HashMap<>();
            content.put("text",m.remove("text"));
            content.put("images",m.remove("images"));
            m.put("content",content);
            Date timestamp = (Date) m.get("timestamp");
            long time = ChronoUnit.MINUTES.between(Instant.ofEpochMilli(timestamp.getTime()),now);

            if(time <= 2){
                m.put("timestamp","刚刚");
            }else if (time > 2 && time <= 60){
                m.put("timestamp",time + " 分钟之前");
            }else if (time > 60 && time / 60 < 24){
                m.put("timestamp",time /60 + " 小时之前");
            }else{
                m.put("timestamp",time /1440 + " 天之前");
            }
        };
        return list;
    }

    @Autowired
    private PointLikeMapper pointLikeMapper;

    @Override
    public void saveOrDeleteIsLike(PointLike pointLike, Integer islike) throws Exception {
        PointLikeExample example = new PointLikeExample();
        example.createCriteria().andCommunicateIdEqualTo(pointLike.getCommunicateId())
                .andUserIdEqualTo(pointLike.getUserId()).andUsernameEqualTo(pointLike.getUsername());
        int size = pointLikeMapper.selectByExample(example).size();
        System.out.println(islike==0);
        if(islike.equals(0)){
//            新增点赞列表
            if(size > 0){
                throw new Exception("已经点赞，不要重复操作");
            }
            pointLikeMapper.insertSelective(pointLike);
        }else{

            if (size == 0) {
                throw new Exception("没有找到点赞记录");
            }
            pointLikeMapper.deleteByExample(example);
        }
    }

    @Autowired
    private CommunicateCommentMapper communicateCommentMapper;
    @Autowired
    private CommunicateContextMapper communicateContextMapper;
    @Override
    public void saveDetialContent(CommunicateComment communicateComment, CommunicateContext communicateContext) throws Exception {
        System.out.println(communicateContext);
        System.out.println(communicateComment);

        if(communicateComment.getCommentId() == 0){
            communicateCommentMapper.insertSelective(communicateComment);
        }


        Integer commentId = communicateComment.getCommentId();
        communicateContext.setCommentId(commentId);
        communicateContext.setTime(new Date());

        communicateContextMapper.insertSelective(communicateContext);
    }
}
